package com.itcast.controller;

import com.itcast.domain.Shopping;
import com.itcast.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("/shopping")
public class ShoppingController {
    @Autowired
    private ShoppingService shoppingService;

    /**
     * 添加商品至购物车
     * 如果I购物车中有该商品，则数量加一，若没有则创建该商品
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addShop/{id}", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addShop(@PathVariable("id") String id) throws Exception {
        return shoppingService.addShop(id);
    }

    /**
     * 删除购物车中的商品
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deleteShop/{id}", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deleteShop(@PathVariable("id") String id) throws Exception {
        return shoppingService.deleteShop(id);
    }

    @RequestMapping(value = "/deleteAllShop", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deleteAllShop() throws Exception {
        return shoppingService.deleteAllShop();
    }

    /**
     * 查询购物车中所有商品
     * @return
     */
    @RequestMapping(value = "/findAll", produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<Shopping> findAll(){
        return shoppingService.findAll();
    }

    /**
     * 通过id查询某个商品
     * @return
     */
    @RequestMapping(value = "/findById", produces = "application/json;charset=utf-8")
    @ResponseBody
    public List<Shopping> findById(){
        return shoppingService.findAll();
    }


}
